package com.pb.camera.jph.activity;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.NetworkInfo;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.pb.camera.R;
import com.pb.camera.adapter.IPCWifiInfoAdapter;
import com.pb.camera.broadcast.WifiBroadcastReceiver;
import com.pb.camera.broadcast.WifiSettingProxy;
import com.pb.camera.constants.MyContants;
import com.pb.camera.utils.Logger;
import com.pb.camera.utils.TaskHelper;
import com.pb.camera.view.ProgressDialog;

import java.util.ArrayList;
import java.util.List;

public class SelectCameraActivity extends Activity implements AdapterView.OnItemClickListener{

    private static final String TAG = "SelectCameraActivity";
    private WifiAdmin mWifiAdmin;
    private List<ScanResult> mIPCWifiList;
    private ListView mLVIPCWifi;
    private ProgressDialog mProgressDialog;
    private WifiBroadcastReceiver mBroadcastReceiver;
    private ScanResult mSelectedScanResult;
    private WifiSettingProxy mWifiProxy;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_camera);
        mLVIPCWifi = (ListView) findViewById(R.id.lv_ipc_wifi);
        mLVIPCWifi.setOnItemClickListener(this);
        init();
    }

    private void init() {
        mProgressDialog = new ProgressDialog(this, false);
        mWifiAdmin = new WifiAdmin();
        TaskHelper.exec(new TaskHelper.Task() {
            @Override
            public void execute() throws Exception {
                mWifiAdmin.startScan();
            }

            @Override
            public void callback(Exception e) {
                mIPCWifiList = new ArrayList<>();
                List<ScanResult> scanResults = mWifiAdmin.getWifiList();
                for (ScanResult scanResult : scanResults) {
                    if (scanResult.SSID.contains("IPC")) {
                        mIPCWifiList.add(scanResult);
                        Logger.d(TAG, scanResult.SSID);
                    }
                }
                IPCWifiInfoAdapter adapter = new IPCWifiInfoAdapter(mIPCWifiList, SelectCameraActivity.this);
                mLVIPCWifi.setAdapter(adapter);
            }
        });
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        mSelectedScanResult = mIPCWifiList.get(position);
        mWifiProxy = new WifiSettingProxy(SelectCameraActivity.this, mSelectedScanResult);
        Intent intent = new Intent(this,SettingCameraWifiActivity.class);
        intent.putExtra("UUID",getIntent().getStringExtra("UUID"));
        mWifiProxy.setIntent(intent);
        mProgressDialog.show();
        TaskHelper.exec(new TaskHelper.Task() {
            boolean result;

            @Override
            public void execute() throws Exception {
                result = mWifiAdmin.connectWifi(mSelectedScanResult, MyContants.DEFAULT_AP_PASSWORD);
            }

            @Override
            public void callback(Exception e) {
                mProgressDialog.dismiss();
                if (result) {
                } else {
                    Toast.makeText(SelectCameraActivity.this, R.string.ap_set_failed, Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    public void finishActivity(View v) {
        finish();
    }

    /*   private boolean isReseting = false;

       @Override
       public void onConnect(String ssid) {
           if (mSelectedScanResult == null)
               return;
           String targetSSID = "\""+mSelectedScanResult.SSID+"\"";
           //没有正确连接到指定wifi，就去让用户手动去设置
           if (isReseting && !ssid.equals(targetSSID) && !mIsJumped){
               Intent wifiSettingsIntent = new Intent("android.settings.WIFI_SETTINGS");
               startActivity(wifiSettingsIntent);
           }else if(ssid.equals(targetSSID)){
               Intent intent = new Intent(SelectCameraActivity.this, SettingCameraWifiActivity.class);
               intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
               startActivity(intent);
               unregisterReceiver(mBroadcastReceiver);
           }
       }

       @Override
       public void onDisconnect() {
           isReseting = true;
       }


       private boolean mIsJumped = false;
       @Override
       protected void onStop() {
           super.onStop();
           mIsJumped = isReseting;
       }*/
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mWifiProxy != null)
            mWifiProxy.finishProxy();
    }

}

